package ru.isys.yoomee.ui;

import java.util.ArrayList;
import java.util.List;

import ru.isys.yoomee.R;
import ru.isys.yoomee.model.Venue;
import ru.isys.yoomee.model.Yoo;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.maps.MapActivity;

public class VenueDetailsActivity extends MapActivity{

    protected static final String VENUE = "venue";
    private ArrayList<Yoo> mYoos;
    private Venue mVenue;
    private View mVenueView;
    private TextView mVenueName;
    private TextView mVenueDistanceTo;
    
    private ListView mNearbyYoosList;
    
    @Override
    protected void onCreate(Bundle arg0)
    {
        
        super.onCreate(arg0);
        setContentView(R.layout.venue_details_layout);
        
        mYoos=new ArrayList<Yoo>();
        mVenueView=findViewById(R.id.target_venue);
        mVenueName= (TextView) mVenueView.findViewById(R.id.venue_name);
        mVenueDistanceTo=(TextView) mVenueView.findViewById(R.id.distance_to);        
        mNearbyYoosList = (ListView) findViewById(android.R.id.list);
        
        mockNearbyYoos();
        
        mNearbyYoosList.setAdapter(new NerbyYoosListAdapter(this,R.layout.venue_details_nearby_yoos_list_item, mYoos));
        Bundle extras;
        if ((extras=getIntent().getExtras())!=null){
            mVenue= (Venue) extras.getSerializable(VENUE);
            if (mVenue!=null){
                mVenueName.setText(mVenue.getNameOfVenue());
                mVenueDistanceTo.setText(mVenue.getDistanceToVenueString());
            }           
        }       
    }
    
    private void mockNearbyYoos(){
        mYoos.add(new Yoo("Nikka", 12, 31,false));
        mYoos.get(0).setLastCheckinVenue(new Venue("Starbucks"));
        
        mYoos.add(new Yoo("Annet"));
        mYoos.get(1).setLastCheckinVenue(new Venue("McDonalds"));
        
        mYoos.add(new Yoo("Humpty Dumpty"));
        mYoos.get(2).setLastCheckinVenue(new Venue("AppStore"));
        
        mYoos.add(new Yoo("Lex"));
        mYoos.get(3).setLastCheckinVenue(new Venue("Cementary"));
        
        mYoos.add(new Yoo("Starbucks"));
        mYoos.get(4).setLastCheckinVenue(new Venue("Starbucks"));
        
        mYoos.add(new Yoo("Some name"));
        mYoos.get(5).setLastCheckinVenue(new Venue("13 Street"));
        
        mYoos.add(new Yoo("Chuck Norris"));
        mYoos.get(6).setLastCheckinVenue(new Venue("World"));
        
        mYoos.add(new Yoo("Mc Donalds"));
        mYoos.get(7).setLastCheckinVenue(new Venue("Starbucks"));
    }

    @Override
    protected boolean isRouteDisplayed()
    {
        
        return false;
    }
    
    private class NerbyYoosListAdapter extends ArrayAdapter<Yoo>
    {

        

        private Context mContext;
        private ArrayList<Yoo> mYoos;
        private LayoutInflater mLayoutInflater;
        private TextView mNickname;
        private TextView mDistanceToYoo;
        private TextView mLastCheckinAgo;
        private TextView mLastCheckin;
        private Button mChatButton;
        private View mView;

        public NerbyYoosListAdapter(Context context, int textViewResourceId, List<Yoo> objects)
        {
            super(context, textViewResourceId, objects);
            mYoos=(ArrayList<Yoo>) objects;
            
        }

        public int getCount()
        {
            return mYoos.size();
        }

        public Yoo getItem(int position)
        {
            return this.mYoos.get(position);
        }

        public long getItemId(int position)
        {
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            
            
            if (convertView == null)
            { // if it's not recycled, initialize some attributes
                mLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                mView = mLayoutInflater.inflate(R.layout.venue_details_nearby_yoos_list_item, null);
            }
            else
            {
                mView = (View) convertView;
            }

            mChatButton=(Button) mView.findViewById(R.id.yoo_chat_with);
            Yoo yoo = mYoos.get(position);

            if (yoo != null)
            {
                mNickname = (TextView) mView.findViewById(R.id.yoo_nickname);
                mDistanceToYoo = (TextView) mView.findViewById(R.id.yoo_distance_to);
                mLastCheckinAgo = (TextView) mView.findViewById(R.id.yoo_checkin_ago);
                mLastCheckin = (TextView) mView.findViewById(R.id.yoo_last_checkin);
                
                
                if (mNickname != null)
                {
                    mNickname.setText(yoo.getNickName());
                }

                if (mDistanceToYoo != null)
                {
                    mDistanceToYoo.setText(yoo.getDistanceToYouString());
                }
                if (mLastCheckinAgo != null)
                {
                    mLastCheckinAgo.setText(yoo.getLastCheckinAgo());
                }
                if (mLastCheckin !=null){
                    mLastCheckin.setText(yoo.getLastCheckinVenue().getNameOfVenue());
                }
            }
            
            mChatButton.setOnClickListener(new OnClickListener()
            {
                
                @Override
                public void onClick(View v)
                {
                    Log.d("onClick", "on chat button click");
                    
                }
            });
            
            
            return mView;
        }       
        
    }
    
    
}